package cn.iocoder.yudao.module.ai.controller.admin.modelfactoryparam;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ai.controller.admin.modelfactoryparam.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.modelfactoryparam.ModelFactoryParamDO;
import cn.iocoder.yudao.module.ai.service.modelfactoryparam.ModelFactoryParamService;

@Tag(name = "管理后台 - 业务模型参数")
@RestController
@RequestMapping("/ai/model-factory-param")
@Validated
public class ModelFactoryParamController {

    @Resource
    private ModelFactoryParamService modelFactoryParamService;

    @PostMapping("/create")
    @Operation(summary = "创建业务模型参数")
    @PreAuthorize("@ss.hasPermission('ai:model-factory-param:create')")
    public CommonResult<Integer> createModelFactoryParam(@Valid @RequestBody ModelFactoryParamSaveReqVO createReqVO) {
        return success(modelFactoryParamService.createModelFactoryParam(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新业务模型参数")
    @PreAuthorize("@ss.hasPermission('ai:model-factory-param:update')")
    public CommonResult<Boolean> updateModelFactoryParam(@Valid @RequestBody ModelFactoryParamSaveReqVO updateReqVO) {
        modelFactoryParamService.updateModelFactoryParam(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除业务模型参数")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:model-factory-param:delete')")
    public CommonResult<Boolean> deleteModelFactoryParam(@RequestParam("id") Integer id) {
        modelFactoryParamService.deleteModelFactoryParam(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得业务模型参数")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:model-factory-param:query')")
    public CommonResult<ModelFactoryParamRespVO> getModelFactoryParam(@RequestParam("id") Integer id) {
        ModelFactoryParamDO modelFactoryParam = modelFactoryParamService.getModelFactoryParam(id);
        return success(BeanUtils.toBean(modelFactoryParam, ModelFactoryParamRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得业务模型参数分页")
    @PreAuthorize("@ss.hasPermission('ai:model-factory-param:query')")
    public CommonResult<PageResult<ModelFactoryParamRespVO>> getModelFactoryParamPage(@Valid ModelFactoryParamPageReqVO pageReqVO) {
        PageResult<ModelFactoryParamDO> pageResult = modelFactoryParamService.getModelFactoryParamPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ModelFactoryParamRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出业务模型参数 Excel")
    @PreAuthorize("@ss.hasPermission('ai:model-factory-param:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportModelFactoryParamExcel(@Valid ModelFactoryParamPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ModelFactoryParamDO> list = modelFactoryParamService.getModelFactoryParamPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "业务模型参数.xls", "数据", ModelFactoryParamRespVO.class,
                        BeanUtils.toBean(list, ModelFactoryParamRespVO.class));
    }

}